﻿using System;
using System.Collections.Generic;
using System.EnterpriseServices;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Services;
using System.Xml.Serialization;
using ESB;
using ESB.YWXT;
using NUnit.Framework;
using YW.BLL.HYXT;
using Hxj.Data;
using System.Data;
using HM = Hxj.Model;
using System.Configuration;
namespace ZYXCESB
{
    /// <summary>
    /// Service1 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class EsbServers : System.Web.Services.WebService
    {
        /// <summary>
        /// 会员充值
        /// </summary>
        /// <param name="xml"></param>
        /// <returns></returns>
        [WebMethod(TransactionOption = TransactionOption.Required)]
        public string HY_Pub_Postcz(string xml)
        {
            Cz e = null;
            #region 返回信息
            ESB.EsbMsg esbmsg = new EsbMsg();
            #endregion
            #region
            if (!string.IsNullOrEmpty(xml) && xml != "test")
            {
                try
                {
                    e = XmlHelper.XmlDeserialize<Cz>(System.Web.HttpUtility.HtmlDecode(xml));
                }
                catch
                {
                    esbmsg.Status = "F";
                    esbmsg.Msg = "参数传入有误！";
                    return System.Web.HttpUtility.HtmlDecode(XmlHelper.XmlSerialize(esbmsg, true));
                }
            }
            else
            {
                e = new Cz();
                e.Ext = "HIS@@123";
                e.Empi = "bfd111e1-bf7b-4971-aa73-5da61cca70d4";
                e.Je = 1;
                e.Type = 1;
                e.Memo = "充值类型：His预缴费 = 0,员工冲餐费=1,会员充值=2,第三方应用=3";

            }
            #endregion
            string strxml = XmlHelper.XmlSerialize(e, true);

            #region
            DbSession dbSessionDefault = GetDbSession();
            Hxj.Model.yw_hyyeb yeb = dbSessionDefault.From<Hxj.Model.yw_hyyeb>().Where(Hxj.Model.yw_hyyeb._.EMPI == e.Empi).ToFirst();

            #endregion

            try
            {
                if (xml == "test")
                {
                    esbmsg.Status = "X";
                    esbmsg.Msg = strxml;
                }
                else
                {
                    esbmsg.Status = "T";
                    //修改会员余额
                    YW.Model.HYXT.yw_hyyeb hyyeb = new YW.Model.HYXT.yw_hyyeb();
                    YW.BLL.HYXT.yw_hyyeb bll = new YW.BLL.HYXT.yw_hyyeb();
                    esbmsg.Status = "T";
                    if (e.Ext != "HIS@@123")
                    {
                        esbmsg.Msg = "无法使用接口！请联系管理人员！";
                        throw new Exception();
                    }
                    if (e.Je < 0)
                    {
                        esbmsg.Msg = "充值金额不能为负！";
                        throw new Exception();
                    }
                    if (e.Type > 3 || e.Type < 0)
                    {
                        esbmsg.Msg = "充值类型不存在！";
                        throw new Exception();
                    }
                    if (yeb == null)
                    {
                        esbmsg.Msg = "新增EMPI,并充值";
                        hyyeb.EMPI = e.Empi;
                        hyyeb.xyye = e.Je;
                        bll.Add(hyyeb);
                    }
                    else
                    {
                        if (BMS.HasFather(yeb.EMPI))
                        {
                            strxml = "子卡不允许充值";
                            throw new Exception("");
                        }
                        esbmsg.Msg = "充值";
                        hyyeb.EMPI = yeb.EMPI;
                        hyyeb.yw_id = yeb.yw_id;
                        hyyeb.xyye = yeb.xyye + e.Je;
                        hyyeb.xyjf = yeb.xyjf;
                        bll.Update(hyyeb);
                    }
                    //增加日志记录
                    YW.Model.HYXT.YW_yebgjl yebgjl = new YW.Model.HYXT.YW_yebgjl();
                    yebgjl.yw_id = Guid.NewGuid();
                    yebgjl.ywlx = e.Type.ToString();
                    yebgjl.EMPI = e.Empi;
                    yebgjl.bgje = e.Je;
                    yebgjl.bglx = e.Type.ToString();
                    YW.BLL.HYXT.YW_yebgjl yebgbll = new YW.BLL.HYXT.YW_yebgjl();
                    yebgbll.Add(yebgjl);

                    strxml = XmlHelper.XmlSerialize(e, true);

                    ContextUtil.SetComplete(); //提交事务 
                }
            }
            catch (Exception ex)
            {
                esbmsg.Status = "F";
                if (string.IsNullOrEmpty(esbmsg.Msg)) esbmsg.Msg = strxml;// ex.Message;
                ContextUtil.SetAbort(); //回滚事务
            }
            //esbmsg.Msg = System.Web.HttpUtility.HtmlEncode(strxml);
            return System.Web.HttpUtility.HtmlDecode(XmlHelper.XmlSerialize(esbmsg, true));
        }

        /// <summary>
        /// 会员消费His确认扣款
        /// </summary>
        /// <param name="xml"></param>
        /// <returns></returns>
        [WebMethod(TransactionOption = TransactionOption.Required)]
        public string HY_Pub_PostxfHis(string xml)
        {
            Cz e = null;
            #region 返回信息
            ESB.EsbMsg esbmsg = new EsbMsg();
            #endregion
            #region
            if (!string.IsNullOrEmpty(xml) && xml != "test")
            {
                try
                {
                    e = XmlHelper.XmlDeserialize<Cz>(System.Web.HttpUtility.HtmlDecode(xml));
                }
                catch
                {
                    esbmsg.Status = "F";
                    esbmsg.Msg = "参数传入有误！";
                    return System.Web.HttpUtility.HtmlDecode(XmlHelper.XmlSerialize(esbmsg, true));
                }
            }
            else
            {
                e = new Cz();
                e.Ext = "HIS@@123";
                e.Empi = "bfd111e1-bf7b-4971-aa73-5da61cca70d4";
                e.Je = 1;
                e.Jf = 1;
                e.Type = 5;
                e.Memo = GetMemo() + @"Action:1001 代表预扣款,1002 代表确认扣款";
                e.Action = "1001";
                e.HisLSH = "HIS结帐编号";

            }
            #endregion
            string strxml = XmlHelper.XmlSerialize(e, true);

            #region
            DbSession dbSessionDefault = GetDbSession();
            Hxj.Model.yw_hyyeb yeb = dbSessionDefault.From<Hxj.Model.yw_hyyeb>().Where(Hxj.Model.yw_hyyeb._.EMPI == e.Empi).ToFirst();

            //YW.Model.HYXT.yw_hyyeb model = new YW.Model.HYXT.yw_hyyeb();

            //model.EMPI = e.Hyyeb.EMPI;
            //model.xyjf = e.Hyyeb.xyjf;
            //model.xyye = e.Hyyeb.xyye;
            //model.yw_id = e.Hyyeb.yw_id;
            #endregion
            DbTrans trans = dbSessionDefault.BeginTransaction();
            try
            {
                if (xml == "test")
                {
                    esbmsg.Status = "X";
                    esbmsg.Msg = strxml;
                }
                else
                {
                    esbmsg.Status = "T";
                    if (e.Ext != "HIS@@123")
                    {
                        esbmsg.Msg = "无法使用接口！请联系管理人员！";
                        throw new Exception();
                    }
                    if (string.IsNullOrEmpty(e.HisLSH))
                    {
                        esbmsg.Msg = "HIS结帐编号不能为空!";
                        throw new Exception();
                    }
                    if (e.Je < 0)
                    {
                        esbmsg.Msg = "金额不能为负！";
                        throw new Exception();
                    }
                    if (e.Type != 5 && e.Type != 6)
                    {
                        esbmsg.Msg = "消费类型参数有误，5 His门诊预缴费,6 His住院预缴费";
                        throw new Exception();
                    }

                    if (yeb == null)
                    {
                        esbmsg.Status = "F";
                        esbmsg.Msg = "EMPI号不存在！";
                    }
                    else
                    {
                        #region
                        ESB.EsbJfMsg s = null;
                        s = BMS.CheckJf(e.Empi, e.Jf.ToString(), e.Je.ToString());
                        esbmsg.Status = s.Status;
                        esbmsg.Msg = s.Msg;
                        if (s.Status == "F")
                        {
                            throw new Exception();
                        }
                        #endregion
                        if (!string.IsNullOrEmpty(yeb.FEMPI))
                        {
                            Hxj.Model.yw_hyyeb fyeb = dbSessionDefault.From<Hxj.Model.yw_hyyeb>().Where(Hxj.Model.yw_hyyeb._.EMPI == yeb.FEMPI).ToFirst();
                            if (fyeb == null)
                            {
                                esbmsg.Msg = String.Format("{0}的父卡{1}不存在！", yeb.EMPI, yeb.FEMPI);
                                throw new Exception();
                            }
                            else
                            {
                                yeb.xyye = fyeb.xyye;
                                yeb.xyjf = fyeb.xyjf;
                            }
                        }
                        if (e.Action == "1001")
                        {
                            #region 1001
                            if (dbSessionDefault.Exists<HM.Yw_Trial>(HM.Yw_Trial._.xgdjh == e.HisLSH + e.Type.ToString() && HM.Yw_Trial._.type == 1))
                            {
                                esbmsg.Msg = "单据已付款成功，不能再试算！";
                                throw new Exception();
                            }
                            HM.Yw_Trial trial = dbSessionDefault.From<HM.Yw_Trial>().Where(HM.Yw_Trial._.xgdjh == e.HisLSH + e.Type.ToString() && HM.Yw_Trial._.type == 0).ToFirst();
                            if (trial == null)
                            {
                                trial = new HM.Yw_Trial();
                                trial.yw_id = Guid.NewGuid();
                                trial.xgdjh = e.HisLSH + e.Type.ToString();
                                trial.balance = yeb.xyye;
                                trial.receipt = e.Je;
                                #region 折扣额和积分现在待定
                                trial.discount = e.Je - decimal.Parse(s.Money);
                                trial.integral = e.Jf;
                                #endregion
                                trial.received = e.Je - trial.discount;
                                if (yeb.xyye < trial.received)
                                {
                                    trial.card = yeb.xyye;
                                    trial.cash = trial.received - yeb.xyye;
                                }
                                else
                                {
                                    trial.card = trial.received;
                                    trial.cash = 0;
                                }
                                trial.type = 0;
                                trial.EMPI = yeb.EMPI;
                                trial.FEMPI = yeb.FEMPI;
                                trial.ext = e.Ext.Split(new char[] { '@' })[0].ToString();
                                int returnvalue = dbSessionDefault.Insert<HM.Yw_Trial>(trial, trans);
                                if (returnvalue > 0)
                                {
                                    e.Action = "1002";
                                    if (e.trial == null)
                                    {
                                        e.trial = new Trial();
                                    }
                                    e.trial.BCJE = trial.receipt;
                                    e.trial.ZHZF = trial.card;
                                    e.trial.XJZF = trial.cash;
                                    e.trial.YHJE = trial.discount;
                                    e.trial.ZHYE = trial.balance;
                                    e.trial.JF = trial.integral;
                                    e.trial.ZFID = trial.yw_id.ToString();
                                    esbmsg.Msg = XmlHelper.XmlSerialize(e, true);
                                }
                                else
                                {
                                    esbmsg.Msg = "试算有误！";
                                    throw new Exception();
                                }
                            }
                            else
                            {
                                trial.Attach();
                                //                                receipt decimal 9 2     √   本次收款金额 
                                //3 discount decimal 9 2     √   折扣 
                                //4 received decimal 9 2     √   实收金额 
                                //5 card decimal 9 2     √   卡中款处理 
                                //6 cash decimal 9 2     √   现金 
                                //7 balance decimal 9 2     √   卡内余额 
                                //8 type int 4 0     √   状态:0代表试算，1代表付款，2代表退款 
                                //9 integral decimal 9 0     √     
                                trial.balance = yeb.xyye;
                                trial.receipt = e.Je;
                                trial.updatetime = DateTime.Now;
                                #region 折扣额和积分现在待定
                                trial.discount = e.Je - decimal.Parse(s.Money);
                                trial.integral = e.Jf;
                                #endregion
                                trial.received = e.Je - trial.discount;
                                if (yeb.xyye < trial.received)
                                {
                                    trial.card = yeb.xyye;
                                    trial.cash = trial.received - yeb.xyye;
                                }
                                else
                                {
                                    trial.card = trial.received;
                                    trial.cash = 0;
                                }
                                trial.EMPI = yeb.EMPI;
                                trial.FEMPI = yeb.FEMPI;
                                trial.ext = e.Ext.Split(new char[] { '@' })[0].ToString();
                                int returnvalue = dbSessionDefault.Update<HM.Yw_Trial>(trial, trans);
                                if (returnvalue > 0)
                                {
                                    e.Action = "1002";
                                    if (e.trial == null) e.trial = new Trial();
                                    e.trial.BCJE = trial.receipt;
                                    e.trial.ZHZF = trial.card;
                                    e.trial.XJZF = trial.cash;
                                    e.trial.YHJE = trial.discount;
                                    e.trial.ZHYE = trial.balance;
                                    e.trial.JF = trial.integral;
                                    e.trial.ZFID = trial.yw_id.ToString();
                                    esbmsg.Msg = XmlHelper.XmlSerialize(e, true);
                                }
                                else
                                {
                                    esbmsg.Msg = "重新试算有误！";
                                    throw new Exception();
                                }

                            }
                            #endregion
                        }
                        else if (e.Action == "1002")
                        {
                            if (e.trial == null)
                            {
                                esbmsg.Msg = "参数传入有误！";
                                throw new Exception();
                            }
                            if (dbSessionDefault.Exists<HM.Yw_Trial>(HM.Yw_Trial._.xgdjh == e.HisLSH + e.Type.ToString() && HM.Yw_Trial._.type == 1))
                            {
                                esbmsg.Msg = "单据已付款成功，不能再试算！";
                                throw new Exception();
                            }
                            if (!dbSessionDefault.Exists<HM.Yw_Trial>(HM.Yw_Trial._.xgdjh == e.HisLSH + e.Type.ToString() && HM.Yw_Trial._.type == 0))
                            {
                                esbmsg.Msg = "请选试算！";
                                throw new Exception();
                            }
                            if (!dbSessionDefault.Exists<HM.Yw_Trial>(HM.Yw_Trial._.xgdjh == e.HisLSH + e.Type.ToString() && HM.Yw_Trial._.type == 0 && HM.Yw_Trial._.yw_id == e.trial.ZFID))
                            {
                                esbmsg.Msg = "二次握手不成功！";
                                throw new Exception();
                            }
                            HM.Yw_Trial trial = dbSessionDefault.From<HM.Yw_Trial>().Where(HM.Yw_Trial._.xgdjh == e.HisLSH + e.Type.ToString() && HM.Yw_Trial._.type == 0 && HM.Yw_Trial._.yw_id == e.trial.ZFID).ToFirst();
                            if (trial != null)
                            {
                                if (
                                e.trial.BCJE == trial.receipt &&
                                e.trial.ZHZF == trial.card &&
                                e.trial.XJZF == trial.cash &&
                                e.trial.YHJE == trial.discount &&
                                e.trial.ZHYE == trial.balance &&
                                e.trial.JF == trial.integral && e.Empi == trial.EMPI)
                                {
                                    #region

                                    #endregion
                                    Guid bgid = Guid.NewGuid();
                                    trial.Attach();
                                    trial.type = 1;
                                    //trial.xgdjh = bgid.ToString();
                                    int i = dbSessionDefault.Update<HM.Yw_Trial>(trial, trans);
                                    if (i > 0)
                                    {
                                        esbmsg.Msg = "ok";
                                        #region 扣款，写日志记录
                                        yeb.Attach();
                                        #region
                                        if (!string.IsNullOrEmpty(yeb.FEMPI))
                                        {
                                            Hxj.Model.yw_hyyeb fyeb = dbSessionDefault.From<Hxj.Model.yw_hyyeb>().Where(Hxj.Model.yw_hyyeb._.EMPI == yeb.FEMPI).ToFirst();
                                            if (fyeb == null)
                                            {
                                                esbmsg.Msg = String.Format("{0}的父卡{1}不存在！", yeb.EMPI, yeb.FEMPI);
                                                throw new Exception();
                                            }
                                            else
                                            {
                                                yeb.xyye = fyeb.xyye;
                                                yeb.xyjf = fyeb.xyjf;
                                            }
                                        }
                                        #endregion
                                        decimal? d = yeb.xyye;
                                        decimal? d1 = yeb.xyye - trial.card;
                                        yeb.xyye = yeb.xyye - trial.card;
                                        yeb.xyjf = yeb.xyjf - trial.integral;
                                        if (d != d1)
                                        {
                                            if (string.IsNullOrEmpty(yeb.FEMPI))
                                            {
                                                i = dbSessionDefault.Update<HM.yw_hyyeb>(yeb, HM.yw_hyyeb._.EMPI == e.Empi && HM.yw_hyyeb._.xyye == trial.balance, trans);
                                            }
                                            else
                                            {
                                                i = dbSessionDefault.Update<HM.yw_hyyeb>(yeb, HM.yw_hyyeb._.EMPI == yeb.FEMPI && HM.yw_hyyeb._.xyye == trial.balance, trans);
                                            }
                                        }
                                        else
                                            i = 1;
                                        if (i > 0)
                                        {
                                            esbmsg.Msg = "ok";
                                            #region 写余额变更记录
                                            Hxj.Model.YW_yebgjl bgjl = new HM.YW_yebgjl();
                                            bgjl.Attach();
                                            bgjl.yw_id = bgid;
                                            bgjl.EMPI = e.Empi;
                                            bgjl.FEMPI = yeb.FEMPI;
                                            bgjl.bgje = e.trial.ZHZF;
                                            bgjl.cash = e.trial.XJZF;
                                            bgjl.integral = e.trial.JF;
                                            bgjl.receipt = e.trial.BCJE;
                                            bgjl.bglx = e.Type.ToString();
                                            bgjl.xgdjh = e.HisLSH;
                                            bgjl.ext = e.Ext.Split(new char[] { '@' })[0].ToString();
                                            bgjl.trialid = new Guid(e.trial.ZFID);
                                            i = dbSessionDefault.Insert<Hxj.Model.YW_yebgjl>(bgjl, trans);
                                            if (i > 0)
                                            {
                                                e.trial.ZFID = bgid.ToString();
                                                e.trial.ZHYE = yeb.xyye;
                                                esbmsg.Msg = XmlHelper.XmlSerialize(e, true);
                                            }
                                            else
                                            {
                                                esbmsg.Msg = "确认扣款不成功！写余额变更记录的时候出错！";
                                                throw new Exception();
                                            }

                                            #endregion

                                        }
                                        else
                                        {
                                            esbmsg.Msg = "请重新试算！确认扣款不成功！会员余额和试算的时候不一致！";
                                            throw new Exception();
                                        }
                                        #endregion
                                    }
                                    else
                                    {
                                        esbmsg.Msg = "确认扣款不成功！";
                                        throw new Exception();
                                    }
                                }
                                else
                                {
                                    esbmsg.Msg = "数据发生变更，请重新试算！";
                                    throw new Exception();
                                }
                            }
                            else
                            {
                                esbmsg.Msg = "....！";
                                throw new Exception();
                            }
                        }
                        else
                        {
                            esbmsg.Msg = "Action参数有误！";
                            throw new Exception();
                        }
                        ////修改会员余额
                        //YW.Model.HYXT.yw_hyyeb hyyeb = new YW.Model.HYXT.yw_hyyeb();
                        //hyyeb.EMPI = yeb.EMPI;
                        //hyyeb.yw_id = yeb.yw_id;
                        //hyyeb.xyye = yeb.xyye - e.Je;
                        //hyyeb.xyjf = yeb.xyjf;
                        //YW.BLL.HYXT.yw_hyyeb bll = new YW.BLL.HYXT.yw_hyyeb();
                        //bll.Update(hyyeb);

                        ////增加日志记录
                        //YW.Model.HYXT.YW_yebgjl yebgjl = new YW.Model.HYXT.YW_yebgjl();
                        //yebgjl.yw_id = Guid.NewGuid();
                        //yebgjl.ywlx = e.Type.ToString();
                        //yebgjl.EMPI = e.Empi;
                        //yebgjl.bgje = -e.Je;
                        //yebgjl.bglx = e.Type.ToString();
                        //YW.BLL.HYXT.YW_yebgjl yebgbll = new YW.BLL.HYXT.YW_yebgjl();
                        //yebgbll.Add(yebgjl);

                        strxml = XmlHelper.XmlSerialize(e, true);
                    }

                    //ContextUtil.SetComplete(); //提交事务 
                    trans.Commit();
                }
            }
            catch (Exception ex)
            {
                esbmsg.Status = "F";
                if (string.IsNullOrEmpty(esbmsg.Msg)) esbmsg.Msg = strxml;// ex.Message;
                //ContextUtil.SetAbort(); //回滚事务
                trans.Rollback();
            }
            finally
            {
                trans.Close();
            }
            return System.Web.HttpUtility.HtmlDecode(XmlHelper.XmlSerialize(esbmsg, true));
        }

        /// <summary>
        /// 会员消费His预扣费
        /// </summary>
        /// <param name="xml"></param>
        /// <returns></returns>
        [WebMethod(TransactionOption = TransactionOption.Required)]
        public string HY_Pub_PostxfHisTrial(string xml)
        {
            Cz e = null;
            #region 返回信息
            ESB.EsbMsg esbmsg = new EsbMsg();
            #endregion
            #region
            if (!string.IsNullOrEmpty(xml) && xml != "test")
            {
                try
                {
                    e = XmlHelper.XmlDeserialize<Cz>(System.Web.HttpUtility.HtmlDecode(xml));
                }
                catch
                {
                    esbmsg.Status = "F";
                    esbmsg.Msg = "参数传入有误！";
                    return System.Web.HttpUtility.HtmlDecode(XmlHelper.XmlSerialize(esbmsg, true));
                }
            }
            else
            {
                e = new Cz();
                e.Ext = "HIS@@123";
                e.Empi = "bfd111e1-bf7b-4971-aa73-5da61cca70d4";
                e.Je = 1;
                e.Jf = 1;
                e.Type = 5;
                e.Memo = GetMemo() + @"Action:1001 代表预扣款,1002 代表确认扣款";
                e.Action = "1001";
                e.HisLSH = "HIS结帐编号";

            }
            #endregion
            string strxml = XmlHelper.XmlSerialize(e, true);

            #region
            DbSession dbSessionDefault = GetDbSession();
            Hxj.Model.yw_hyyeb yeb = dbSessionDefault.From<Hxj.Model.yw_hyyeb>().Where(Hxj.Model.yw_hyyeb._.EMPI == e.Empi).ToFirst();

            //YW.Model.HYXT.yw_hyyeb model = new YW.Model.HYXT.yw_hyyeb();

            //model.EMPI = e.Hyyeb.EMPI;
            //model.xyjf = e.Hyyeb.xyjf;
            //model.xyye = e.Hyyeb.xyye;
            //model.yw_id = e.Hyyeb.yw_id;
            #endregion
            DbTrans trans = dbSessionDefault.BeginTransaction();
            try
            {
                if (xml == "test")
                {
                    esbmsg.Status = "X";
                    esbmsg.Msg = strxml;
                }
                else
                {
                    esbmsg.Status = "T";
                    if (e.Ext != "HIS@@123")
                    {
                        esbmsg.Msg = "无法使用接口！请联系管理人员！";
                        throw new Exception();
                    }
                    if (string.IsNullOrEmpty(e.HisLSH))
                    {
                        esbmsg.Msg = "HIS结帐编号不能为空!";
                        throw new Exception();
                    }
                    if (e.Je < 0)
                    {
                        esbmsg.Msg = "金额不能为负！";
                        throw new Exception();
                    }
                    if (e.Type != 5 && e.Type != 6)
                    {
                        esbmsg.Msg = "消费类型参数有误，5 His门诊预缴费,6 His住院预缴费";
                        throw new Exception();
                    }

                    if (yeb == null)
                    {
                        esbmsg.Status = "F";
                        esbmsg.Msg = "EMPI号不存在！";
                    }
                    else
                    {
                        #region
                        ESB.EsbJfMsg s = null;
                        s = BMS.CheckJf(e.Empi, e.Jf.ToString(), e.Je.ToString());
                        esbmsg.Status = s.Status;
                        esbmsg.Msg = s.Msg;
                        if (s.Status == "F")
                        {
                            throw new Exception();
                        }
                        #endregion
                        if (!string.IsNullOrEmpty(yeb.FEMPI))
                        {
                            Hxj.Model.yw_hyyeb fyeb = dbSessionDefault.From<Hxj.Model.yw_hyyeb>().Where(Hxj.Model.yw_hyyeb._.EMPI == yeb.FEMPI).ToFirst();
                            if (fyeb == null)
                            {
                                esbmsg.Msg = String.Format("{0}的父卡{1}不存在！", yeb.EMPI, yeb.FEMPI);
                                throw new Exception();
                            }
                            else
                            {
                                yeb.xyye = fyeb.xyye;
                                yeb.xyjf = fyeb.xyjf;
                            }
                        }
                        if (e.Action == "1001")
                        {
                            #region 1001
                            if (dbSessionDefault.Exists<HM.Yw_Trial>(HM.Yw_Trial._.xgdjh == e.HisLSH + e.Type.ToString() && HM.Yw_Trial._.type == 1))
                            {
                                esbmsg.Msg = "单据已付款成功，不能再试算！";
                                throw new Exception();
                            }
                            HM.Yw_Trial trial = dbSessionDefault.From<HM.Yw_Trial>().Where(HM.Yw_Trial._.xgdjh == e.HisLSH + e.Type.ToString() && HM.Yw_Trial._.type == 0).ToFirst();
                            if (trial == null)
                            {
                                trial = new HM.Yw_Trial();
                                trial.yw_id = Guid.NewGuid();
                                trial.xgdjh = e.HisLSH + e.Type.ToString();
                                trial.balance = yeb.xyye;
                                trial.receipt = e.Je;
                                #region 折扣额和积分现在待定
                                trial.discount = e.Je - decimal.Parse(s.Money);
                                trial.integral = e.Jf;
                                #endregion
                                trial.received = e.Je - trial.discount;
                                if (yeb.xyye < trial.received)
                                {
                                    trial.card = yeb.xyye;
                                    trial.cash = trial.received - yeb.xyye;
                                }
                                else
                                {
                                    trial.card = trial.received;
                                    trial.cash = 0;
                                }
                                trial.type = 0;
                                trial.EMPI = yeb.EMPI;
                                trial.FEMPI = yeb.FEMPI;
                                trial.ext = e.Ext.Split(new char[] { '@' })[0].ToString();
                                int returnvalue = dbSessionDefault.Insert<HM.Yw_Trial>(trial, trans);
                                if (returnvalue > 0)
                                {
                                    e.Action = "1002";
                                    if (e.trial == null)
                                    {
                                        e.trial = new Trial();
                                    }
                                    e.trial.BCJE = trial.receipt;
                                    e.trial.ZHZF = trial.card;
                                    e.trial.XJZF = trial.cash;
                                    e.trial.YHJE = trial.discount;
                                    e.trial.ZHYE = trial.balance;
                                    e.trial.JF = trial.integral;
                                    e.trial.ZFID = trial.yw_id.ToString();
                                    esbmsg.Msg = XmlHelper.XmlSerialize(e, true);
                                }
                                else
                                {
                                    esbmsg.Msg = "试算有误！";
                                    throw new Exception();
                                }
                            }
                            else
                            {
                                trial.Attach();
                                //                                receipt decimal 9 2     √   本次收款金额 
                                //3 discount decimal 9 2     √   折扣 
                                //4 received decimal 9 2     √   实收金额 
                                //5 card decimal 9 2     √   卡中款处理 
                                //6 cash decimal 9 2     √   现金 
                                //7 balance decimal 9 2     √   卡内余额 
                                //8 type int 4 0     √   状态:0代表试算，1代表付款，2代表退款 
                                //9 integral decimal 9 0     √     
                                trial.balance = yeb.xyye;
                                trial.receipt = e.Je;
                                trial.updatetime = DateTime.Now;
                                #region 折扣额和积分现在待定
                                trial.discount = e.Je - decimal.Parse(s.Money);
                                trial.integral = e.Jf;
                                #endregion
                                trial.received = e.Je - trial.discount;
                                if (yeb.xyye < trial.received)
                                {
                                    trial.card = yeb.xyye;
                                    trial.cash = trial.received - yeb.xyye;
                                }
                                else
                                {
                                    trial.card = trial.received;
                                    trial.cash = 0;
                                }
                                trial.EMPI = yeb.EMPI;
                                trial.FEMPI = yeb.FEMPI;
                                trial.ext = e.Ext.Split(new char[] { '@' })[0].ToString();
                                int returnvalue = dbSessionDefault.Update<HM.Yw_Trial>(trial, trans);
                                if (returnvalue > 0)
                                {
                                    e.Action = "1002";
                                    if (e.trial == null) e.trial = new Trial();
                                    e.trial.BCJE = trial.receipt;
                                    e.trial.ZHZF = trial.card;
                                    e.trial.XJZF = trial.cash;
                                    e.trial.YHJE = trial.discount;
                                    e.trial.ZHYE = trial.balance;
                                    e.trial.JF = trial.integral;
                                    e.trial.ZFID = trial.yw_id.ToString();
                                    esbmsg.Msg = XmlHelper.XmlSerialize(e, true);
                                }
                                else
                                {
                                    esbmsg.Msg = "重新试算有误！";
                                    throw new Exception();
                                }

                            }
                            #endregion
                        }
                        else if (e.Action == "1002")
                        {
                            if (e.trial == null)
                            {
                                esbmsg.Msg = "参数传入有误！";
                                throw new Exception();
                            }
                            if (dbSessionDefault.Exists<HM.Yw_Trial>(HM.Yw_Trial._.xgdjh == e.HisLSH + e.Type.ToString() && HM.Yw_Trial._.type == 1))
                            {
                                esbmsg.Msg = "单据已付款成功，不能再试算！";
                                throw new Exception();
                            }
                            if (!dbSessionDefault.Exists<HM.Yw_Trial>(HM.Yw_Trial._.xgdjh == e.HisLSH + e.Type.ToString() && HM.Yw_Trial._.type == 0))
                            {
                                esbmsg.Msg = "请选试算！";
                                throw new Exception();
                            }
                            if (!dbSessionDefault.Exists<HM.Yw_Trial>(HM.Yw_Trial._.xgdjh == e.HisLSH + e.Type.ToString() && HM.Yw_Trial._.type == 0 && HM.Yw_Trial._.yw_id == e.trial.ZFID))
                            {
                                esbmsg.Msg = "二次握手不成功！";
                                throw new Exception();
                            }
                            HM.Yw_Trial trial = dbSessionDefault.From<HM.Yw_Trial>().Where(HM.Yw_Trial._.xgdjh == e.HisLSH + e.Type.ToString() && HM.Yw_Trial._.type == 0 && HM.Yw_Trial._.yw_id == e.trial.ZFID).ToFirst();
                            if (trial != null)
                            {
                                if (
                                e.trial.BCJE == trial.receipt &&
                                e.trial.ZHZF == trial.card &&
                                e.trial.XJZF == trial.cash &&
                                e.trial.YHJE == trial.discount &&
                                e.trial.ZHYE == trial.balance &&
                                e.trial.JF == trial.integral && e.Empi == trial.EMPI)
                                {
                                    #region

                                    #endregion
                                    Guid bgid = Guid.NewGuid();
                                    trial.Attach();
                                    trial.type = 1;
                                    //trial.xgdjh = bgid.ToString();
                                    int i = dbSessionDefault.Update<HM.Yw_Trial>(trial, trans);
                                    if (i > 0)
                                    {
                                        esbmsg.Msg = "ok";
                                        #region 扣款，写日志记录
                                        yeb.Attach();
                                        #region
                                        if (!string.IsNullOrEmpty(yeb.FEMPI))
                                        {
                                            Hxj.Model.yw_hyyeb fyeb = dbSessionDefault.From<Hxj.Model.yw_hyyeb>().Where(Hxj.Model.yw_hyyeb._.EMPI == yeb.FEMPI).ToFirst();
                                            if (fyeb == null)
                                            {
                                                esbmsg.Msg = String.Format("{0}的父卡{1}不存在！", yeb.EMPI, yeb.FEMPI);
                                                throw new Exception();
                                            }
                                            else
                                            {
                                                yeb.xyye = fyeb.xyye;
                                                yeb.xyjf = fyeb.xyjf;
                                            }
                                        }
                                        #endregion
                                        decimal? d = yeb.xyye;
                                        decimal? d1 = yeb.xyye - trial.card;
                                        yeb.xyye = yeb.xyye - trial.card;
                                        yeb.xyjf = yeb.xyjf - trial.integral;
                                        if (d != d1)
                                        {
                                            if (string.IsNullOrEmpty(yeb.FEMPI))
                                            {
                                                i = dbSessionDefault.Update<HM.yw_hyyeb>(yeb, HM.yw_hyyeb._.EMPI == e.Empi && HM.yw_hyyeb._.xyye == trial.balance, trans);
                                            }
                                            else
                                            {
                                                i = dbSessionDefault.Update<HM.yw_hyyeb>(yeb, HM.yw_hyyeb._.EMPI == yeb.FEMPI && HM.yw_hyyeb._.xyye == trial.balance, trans);
                                            }
                                        }
                                        else
                                            i = 1;
                                        if (i > 0)
                                        {
                                            esbmsg.Msg = "ok";
                                            #region 写余额变更记录
                                            Hxj.Model.YW_yebgjl bgjl = new HM.YW_yebgjl();
                                            bgjl.Attach();
                                            bgjl.yw_id = bgid;
                                            bgjl.EMPI = e.Empi;
                                            bgjl.FEMPI = yeb.FEMPI;
                                            bgjl.bgje = e.trial.ZHZF;
                                            bgjl.cash = e.trial.XJZF;
                                            bgjl.integral = e.trial.JF;
                                            bgjl.receipt = e.trial.BCJE;
                                            bgjl.bglx = e.Type.ToString();
                                            bgjl.xgdjh = e.HisLSH;
                                            bgjl.ext = e.Ext.Split(new char[] { '@' })[0].ToString();
                                            bgjl.trialid = new Guid(e.trial.ZFID);
                                            i = dbSessionDefault.Insert<Hxj.Model.YW_yebgjl>(bgjl, trans);
                                            if (i > 0)
                                            {
                                                e.trial.ZFID = bgid.ToString();
                                                e.trial.ZHYE = yeb.xyye;
                                                esbmsg.Msg = XmlHelper.XmlSerialize(e, true);
                                            }
                                            else
                                            {
                                                esbmsg.Msg = "确认扣款不成功！写余额变更记录的时候出错！";
                                                throw new Exception();
                                            }

                                            #endregion

                                        }
                                        else
                                        {
                                            esbmsg.Msg = "请重新试算！确认扣款不成功！会员余额和试算的时候不一致！";
                                            throw new Exception();
                                        }
                                        #endregion
                                    }
                                    else
                                    {
                                        esbmsg.Msg = "确认扣款不成功！";
                                        throw new Exception();
                                    }
                                }
                                else
                                {
                                    esbmsg.Msg = "数据发生变更，请重新试算！";
                                    throw new Exception();
                                }
                            }
                            else
                            {
                                esbmsg.Msg = "....！";
                                throw new Exception();
                            }
                        }
                        else
                        {
                            esbmsg.Msg = "Action参数有误！";
                            throw new Exception();
                        }
                        ////修改会员余额
                        //YW.Model.HYXT.yw_hyyeb hyyeb = new YW.Model.HYXT.yw_hyyeb();
                        //hyyeb.EMPI = yeb.EMPI;
                        //hyyeb.yw_id = yeb.yw_id;
                        //hyyeb.xyye = yeb.xyye - e.Je;
                        //hyyeb.xyjf = yeb.xyjf;
                        //YW.BLL.HYXT.yw_hyyeb bll = new YW.BLL.HYXT.yw_hyyeb();
                        //bll.Update(hyyeb);

                        ////增加日志记录
                        //YW.Model.HYXT.YW_yebgjl yebgjl = new YW.Model.HYXT.YW_yebgjl();
                        //yebgjl.yw_id = Guid.NewGuid();
                        //yebgjl.ywlx = e.Type.ToString();
                        //yebgjl.EMPI = e.Empi;
                        //yebgjl.bgje = -e.Je;
                        //yebgjl.bglx = e.Type.ToString();
                        //YW.BLL.HYXT.YW_yebgjl yebgbll = new YW.BLL.HYXT.YW_yebgjl();
                        //yebgbll.Add(yebgjl);

                        strxml = XmlHelper.XmlSerialize(e, true);
                    }

                    //ContextUtil.SetComplete(); //提交事务 
                    trans.Commit();
                }
            }
            catch (Exception ex)
            {
                esbmsg.Status = "F";
                if (string.IsNullOrEmpty(esbmsg.Msg)) esbmsg.Msg = strxml;// ex.Message;
                //ContextUtil.SetAbort(); //回滚事务
                trans.Rollback();
            }
            finally
            {
                trans.Close();
            }
            return System.Web.HttpUtility.HtmlDecode(XmlHelper.XmlSerialize(esbmsg, true));
        }

        /// <summary>
        /// 会员消费
        /// </summary>
        /// <param name="xml"></param>
        /// <returns></returns>
        [WebMethod(TransactionOption = TransactionOption.Required)]
        public string HY_Pub_Postxf(string xml)
        {
            Cz e = null;
            #region 返回信息
            ESB.EsbMsg esbmsg = new EsbMsg();
            #endregion
            #region
            if (!string.IsNullOrEmpty(xml) && xml != "test")
            {
                try
                {
                    e = XmlHelper.XmlDeserialize<Cz>(System.Web.HttpUtility.HtmlDecode(xml));
                }
                catch
                {
                    esbmsg.Status = "F";
                    esbmsg.Msg = "参数传入有误！";
                    return System.Web.HttpUtility.HtmlDecode(XmlHelper.XmlSerialize(esbmsg, true));
                }
            }
            else
            {
                e = new Cz();
                e.Ext = "HIS@@123";
                e.Empi = "bfd111e1-bf7b-4971-aa73-5da61cca70d4";
                e.Je = 1;
                e.Type = 1;
                e.Memo = "充值类型：His预缴费 = 0,员工冲餐费=1,会员充值=2,第三方应用=3，退款-4";

            }
            #endregion
            string strxml = XmlHelper.XmlSerialize(e, true);

            #region
            DbSession dbSessionDefault = GetDbSession();
            Hxj.Model.yw_hyyeb yeb = dbSessionDefault.From<Hxj.Model.yw_hyyeb>().Where(Hxj.Model.yw_hyyeb._.EMPI == e.Empi).ToFirst();

            //YW.Model.HYXT.yw_hyyeb model = new YW.Model.HYXT.yw_hyyeb();

            //model.EMPI = e.Hyyeb.EMPI;
            //model.xyjf = e.Hyyeb.xyjf;
            //model.xyye = e.Hyyeb.xyye;
            //model.yw_id = e.Hyyeb.yw_id;
            #endregion

            try
            {
                if (xml == "test")
                {
                    esbmsg.Status = "X";
                    esbmsg.Msg = strxml;
                }
                else
                {
                    esbmsg.Status = "T";
                    if (e.Ext != "HIS@@123")
                    {
                        esbmsg.Msg = "无法使用接口！请联系管理人员！";
                        throw new Exception();
                    }
                    if (e.Je < 0)
                    {
                        esbmsg.Msg = "金额不能为负！";
                        throw new Exception();
                    }
                    if (e.Type > 3 || e.Type < 0)
                    {
                        esbmsg.Msg = "类型不存在！";
                        throw new Exception();
                    }

                    if (yeb == null)
                    {
                        esbmsg.Status = "F";
                        esbmsg.Msg = "EMPI号不存在！";
                    }
                    else
                    {
                        //修改会员余额
                        YW.Model.HYXT.yw_hyyeb hyyeb = new YW.Model.HYXT.yw_hyyeb();
                        hyyeb.EMPI = yeb.EMPI;
                        hyyeb.yw_id = yeb.yw_id;
                        hyyeb.xyye = yeb.xyye - e.Je;
                        hyyeb.xyjf = yeb.xyjf;
                        YW.BLL.HYXT.yw_hyyeb bll = new YW.BLL.HYXT.yw_hyyeb();
                        bll.Update(hyyeb);

                        //增加日志记录
                        YW.Model.HYXT.YW_yebgjl yebgjl = new YW.Model.HYXT.YW_yebgjl();
                        yebgjl.yw_id = Guid.NewGuid();
                        yebgjl.ywlx = e.Type.ToString();
                        yebgjl.EMPI = e.Empi;
                        yebgjl.bgje = -e.Je;
                        yebgjl.bglx = e.Type.ToString();
                        YW.BLL.HYXT.YW_yebgjl yebgbll = new YW.BLL.HYXT.YW_yebgjl();
                        yebgbll.Add(yebgjl);

                        strxml = XmlHelper.XmlSerialize(e, true);
                    }

                    ContextUtil.SetComplete(); //提交事务 
                }
            }
            catch (Exception ex)
            {
                esbmsg.Status = "F";
                if (string.IsNullOrEmpty(esbmsg.Msg)) esbmsg.Msg = strxml;// ex.Message;
                ContextUtil.SetAbort(); //回滚事务
            }
            return System.Web.HttpUtility.HtmlDecode(XmlHelper.XmlSerialize(esbmsg, true));
        }

        /// <summary>
        /// 会员退款接口
        /// </summary>
        /// <param name="xml"></param>
        /// <returns></returns>
        [WebMethod(TransactionOption = TransactionOption.Required)]
        public string HY_Pub_Posttk(string xml)
        {
            Cz e = null;
            #region 返回信息
            ESB.EsbMsg esbmsg = new EsbMsg();
            #endregion
            #region
            if (!string.IsNullOrEmpty(xml) && xml != "test")
            {
                try
                {
                    e = XmlHelper.XmlDeserialize<Cz>(System.Web.HttpUtility.HtmlDecode(xml));
                }
                catch
                {
                    esbmsg.Status = "F";
                    esbmsg.Msg = "参数传入有误！";
                    return System.Web.HttpUtility.HtmlDecode(XmlHelper.XmlSerialize(esbmsg, true));
                }
            }
            else
            {
                e = new Cz();
                e.Ext = "HIS@@123";
                e.Empi = "bfd111e1-bf7b-4971-aa73-5da61cca70d4";
                e.Je = 1;
                e.Type = 4;
                e.Memo = GetMemo();

            }
            #endregion
            string strxml = XmlHelper.XmlSerialize(e, true);

            #region
            DbSession dbSessionDefault = GetDbSession();
            Hxj.Model.yw_hyyeb yeb = dbSessionDefault.From<Hxj.Model.yw_hyyeb>().Where(Hxj.Model.yw_hyyeb._.EMPI == e.Empi).ToFirst();

            //YW.Model.HYXT.yw_hyyeb model = new YW.Model.HYXT.yw_hyyeb();

            //model.EMPI = e.Hyyeb.EMPI;
            //model.xyjf = e.Hyyeb.xyjf;
            //model.xyye = e.Hyyeb.xyye;
            //model.yw_id = e.Hyyeb.yw_id;
            #endregion

            try
            {
                if (xml == "test")
                {
                    esbmsg.Status = "X";
                    esbmsg.Msg = strxml;
                }
                else
                {
                    esbmsg.Status = "T";
                    if (e.Ext != "HIS@@123")
                    {
                        esbmsg.Msg = "无法使用接口！请联系管理人员！";
                        throw new Exception();
                    }
                    if (e.Je < 0)
                    {
                        esbmsg.Msg = "金额不能为负！";
                        throw new Exception();
                    }
                    //if (e.Type > 3 || e.Type < 0)
                    //{
                    //    esbmsg.Msg = "类型不存在！";
                    //    throw new Exception();
                    //}
                    if (yeb == null)
                    {
                        esbmsg.Status = "F";
                        esbmsg.Msg = "EMPI号不存在！";
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(yeb.FEMPI))
                        {
                            Hxj.Model.yw_hyyeb fyeb = dbSessionDefault.From<Hxj.Model.yw_hyyeb>().Where(Hxj.Model.yw_hyyeb._.EMPI == yeb.FEMPI).ToFirst();
                            if (fyeb == null)
                            {
                                esbmsg.Msg = String.Format("{0}的父卡{1}不存在！", yeb.EMPI, yeb.FEMPI);
                                throw new Exception();
                            }
                            else
                            {
                                yeb.EMPI = fyeb.FEMPI;
                                yeb.yw_id = fyeb.yw_id;
                                yeb.xyjf = fyeb.xyjf;
                            }
                        }
                        //修改会员余额
                        YW.Model.HYXT.yw_hyyeb hyyeb = new YW.Model.HYXT.yw_hyyeb();
                        hyyeb.EMPI = yeb.EMPI;
                        hyyeb.yw_id = yeb.yw_id;
                        //if (yeb.xyye <e.Je)
                        //{
                        //    esbmsg.Msg = "退款金额不能大于现有金额";
                        //    throw new Exception();
                        //}
                        hyyeb.xyye = yeb.xyye + e.Je;
                        hyyeb.xyjf = yeb.xyjf;
                        YW.BLL.HYXT.yw_hyyeb bll = new YW.BLL.HYXT.yw_hyyeb();
                        bll.Update(hyyeb);

                        //增加日志记录
                        YW.Model.HYXT.YW_yebgjl yebgjl = new YW.Model.HYXT.YW_yebgjl();
                        yebgjl.yw_id = Guid.NewGuid();
                        yebgjl.ywlx = e.Type.ToString();
                        yebgjl.EMPI = e.Empi;
                        yebgjl.bgje = e.Je;
                        yebgjl.bglx = e.Type.ToString() == null ? "4" : "4";
                        YW.BLL.HYXT.YW_yebgjl yebgbll = new YW.BLL.HYXT.YW_yebgjl();
                        yebgbll.Add(yebgjl);

                        strxml = XmlHelper.XmlSerialize(e, true);
                    }

                    ContextUtil.SetComplete(); //提交事务 
                }
            }
            catch (Exception ex)
            {
                esbmsg.Status = "F";
                if (string.IsNullOrEmpty(esbmsg.Msg))
                {
                    esbmsg.Msg = strxml;// ex.Message;
                }
                ContextUtil.SetAbort(); //回滚事务
            }
            return System.Web.HttpUtility.HtmlDecode(XmlHelper.XmlSerialize(esbmsg, true));
        }

        private static string GetMemo()
        {
            return "充值类型：His预缴费 = 0,员工冲餐费=1,会员充值=2,第三方应用=3，退款=4,His门诊预缴费=5, His住院预缴费=6";
        }

        [WebMethod(TransactionOption = TransactionOption.Required)]
        public string HY_Pub_PosttkHis(string xml)
        {
            Cz e = null;
            #region 返回信息
            ESB.EsbMsg esbmsg = new EsbMsg();
            #endregion
            #region
            if (!string.IsNullOrEmpty(xml) && xml != "test")
            {
                try
                {
                    e = XmlHelper.XmlDeserialize<Cz>(System.Web.HttpUtility.HtmlDecode(xml));
                }
                catch
                {
                    esbmsg.Status = "F";
                    esbmsg.Msg = "参数传入有误！";
                    return System.Web.HttpUtility.HtmlDecode(XmlHelper.XmlSerialize(esbmsg, true));
                }
            }
            else
            {
                e = new Cz();
                e.Ext = "HIS@@123";
                e.Empi = "bfd111e1-bf7b-4971-aa73-5da61cca70d4";
                e.Je = 1;
                e.Type = 4;
                e.ZFID = "支付编号，用友支付流水号";
                e.Memo = "充值类型：His预缴费 = 0,员工冲餐费=1,会员充值=2,第三方应用=3，退款-4";

            }
            #endregion
            string strxml = XmlHelper.XmlSerialize(e, true);

            #region
            DbSession dbSessionDefault = GetDbSession();
            Hxj.Model.yw_hyyeb yeb = dbSessionDefault.From<Hxj.Model.yw_hyyeb>().Where(Hxj.Model.yw_hyyeb._.EMPI == e.Empi).ToFirst();

            //YW.Model.HYXT.yw_hyyeb model = new YW.Model.HYXT.yw_hyyeb();

            //model.EMPI = e.Hyyeb.EMPI;
            //model.xyjf = e.Hyyeb.xyjf;
            //model.xyye = e.Hyyeb.xyye;
            //model.yw_id = e.Hyyeb.yw_id;
            #endregion
            DbTrans trans = dbSessionDefault.BeginTransaction();
            try
            {
                if (xml == "test")
                {
                    esbmsg.Status = "X";
                    esbmsg.Msg = strxml;
                }
                else
                {
                    esbmsg.Status = "T";
                    if (e.Ext != "HIS@@123")
                    {
                        esbmsg.Msg = "无法使用接口！请联系管理人员！";
                        throw new Exception();
                    }
                    if (e.Je < 0)
                    {
                        esbmsg.Msg = "金额不能为负！";
                        throw new Exception();
                    }
                    //if (e.Type > 3 || e.Type < 0)
                    //{
                    //    esbmsg.Msg = "类型不存在！";
                    //    throw new Exception();
                    //}
                    if (string.IsNullOrEmpty(e.ZFID))
                    {
                        esbmsg.Msg = "支付编号(用友支付流水号)不能为空";
                        throw new Exception();
                    }
                    if (yeb == null)
                    {
                        esbmsg.Status = "F";
                        esbmsg.Msg = "EMPI号不存在！";
                    }
                    else
                    {
                        Guid gd = Guid.NewGuid();
                        if (!Guid.TryParse(e.ZFID, out gd))
                        {
                            esbmsg.Msg = "ZFID参数格式有误！";
                            throw new Exception();
                        }
                        Hxj.Model.YW_yebgjl bgjl = dbSessionDefault.From<Hxj.Model.YW_yebgjl>().Where(Hxj.Model.YW_yebgjl._.EMPI == e.Empi && Hxj.Model.YW_yebgjl._.yw_id == gd).ToFirst();
                        if (bgjl != null)
                        {
                            if (dbSessionDefault.From<Hxj.Model.YW_yebgjl>().Where(Hxj.Model.YW_yebgjl._.EMPI == e.Empi && Hxj.Model.YW_yebgjl._.xgdjh == gd.ToString() && Hxj.Model.YW_yebgjl._.bglx == "4").ToFirst() != null)
                            {
                                esbmsg.Msg = "不允许重复退款";
                                throw new Exception();
                            }
                            if (bgjl.bgje != e.Je)
                            {
                                esbmsg.Msg = "退款金额不一致，不能退款";
                                throw new Exception();
                            }

                            if (!string.IsNullOrEmpty(yeb.FEMPI))
                            {
                                Hxj.Model.yw_hyyeb fyeb = dbSessionDefault.From<Hxj.Model.yw_hyyeb>().Where(Hxj.Model.yw_hyyeb._.EMPI == yeb.FEMPI).ToFirst();
                                if (fyeb == null)
                                {
                                    esbmsg.Msg = String.Format("{0}的父卡{1}不存在！", yeb.EMPI, yeb.FEMPI);
                                    throw new Exception();
                                }
                                else
                                {
                                    yeb = fyeb;
                                }
                            }
                            yeb.Attach();
                            yeb.lastupdate = DateTime.Now;
                            yeb.xyye = yeb.xyye + e.Je;
                            yeb.xyjf = (yeb.xyjf == null ? 0 : yeb.xyjf) - (bgjl.integral == null ? 0 : bgjl.integral);
                            int i = dbSessionDefault.Update<Hxj.Model.yw_hyyeb>(yeb, trans);
                            if (i > 0)
                            {
                                if (bgjl.ext != e.Ext.Split(new char[] { '@' })[0].ToString())
                                {
                                    esbmsg.Msg = "无退款的权限";
                                    throw new Exception();
                                }
                                bgjl.yw_id = Guid.NewGuid();
                                bgjl.bglx = "4";
                                bgjl.xgdjh = gd.ToString();
                                i = dbSessionDefault.Insert<Hxj.Model.YW_yebgjl>(bgjl, trans);
                                if (i > 0)
                                {
                                    esbmsg.Msg = "退款成功";
                                }
                                else
                                {
                                    esbmsg.Msg = "退款失败";
                                    throw new Exception();
                                }
                                //yebgjl.ywlx = e.Type.ToString();
                                //yebgjl.EMPI = e.Empi;
                                //yebgjl.bgje = e.Je;
                                //yebgjl.bglx = e.Type.ToString() == null ? "4" : "4";
                                //YW.BLL.HYXT.YW_yebgjl yebgbll = new YW.BLL.HYXT.YW_yebgjl();
                            }
                            else
                            {
                                esbmsg.Msg = "会员余额更新失败";
                                throw new Exception();
                            }
                        }
                        else
                        {
                            esbmsg.Msg = "支付编号(用友支付流水号)不存在";
                            throw new Exception();
                        }
                        ////修改会员余额
                        //YW.Model.HYXT.yw_hyyeb hyyeb = new YW.Model.HYXT.yw_hyyeb();
                        //hyyeb.EMPI = yeb.EMPI;
                        //hyyeb.yw_id = yeb.yw_id;
                        ////if (yeb.xyye <e.Je)
                        ////{
                        ////    esbmsg.Msg = "退款金额不能大于现有金额";
                        ////    throw new Exception();
                        ////}
                        //hyyeb.xyye = yeb.xyye + e.Je;
                        //hyyeb.xyjf = yeb.xyjf;
                        //YW.BLL.HYXT.yw_hyyeb bll = new YW.BLL.HYXT.yw_hyyeb();
                        //bll.Update(hyyeb);

                        ////增加日志记录
                        //YW.Model.HYXT.YW_yebgjl yebgjl = new YW.Model.HYXT.YW_yebgjl();
                        //yebgjl.yw_id = Guid.NewGuid();
                        //yebgjl.ywlx = e.Type.ToString();
                        //yebgjl.EMPI = e.Empi;
                        //yebgjl.bgje = e.Je;
                        //yebgjl.bglx = e.Type.ToString() == null ? "4" : "4";
                        //YW.BLL.HYXT.YW_yebgjl yebgbll = new YW.BLL.HYXT.YW_yebgjl();
                        //yebgbll.Add(yebgjl);

                        strxml = XmlHelper.XmlSerialize(e, true);
                    }
                    trans.Commit();
                    //ContextUtil.SetComplete(); //提交事务 
                }
            }
            catch (Exception ex)
            {
                esbmsg.Status = "F";
                if (string.IsNullOrEmpty(esbmsg.Msg))
                {
                    esbmsg.Msg = strxml;// ex.Message;
                }
                // ContextUtil.SetAbort(); //回滚事务
                trans.Rollback();
            }
            finally
            {
                trans.Close();
            }
            return System.Web.HttpUtility.HtmlDecode(XmlHelper.XmlSerialize(esbmsg, true));
        }

        private static DbSession GetDbSession()
        {
            DbSession dbSessionDefault = new DbSession(DatabaseType.SqlServer, System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]);
            return dbSessionDefault;
        }

        /// <summary>
        /// 余额查询
        /// </summary>
        /// <param name="xml"></param>
        /// <returns></returns>
        [WebMethod(TransactionOption = TransactionOption.Required)]
        public string HY_Pub_Postyecx(string xml)
        {
            EsbHy e = null;
            //DbSession dbSessionDefault = new DbSession("ConnectionString");
            // 

            #region 返回信息
            ESB.EsbMsg esbmsg = new EsbMsg();
            #endregion
            #region
            if (!string.IsNullOrEmpty(xml) && xml != "test")
            {
                try
                {
                    e = XmlHelper.XmlDeserialize<EsbHy>(System.Web.HttpUtility.HtmlDecode(xml));
                }
                catch
                {
                    esbmsg.Status = "F";
                    esbmsg.Msg = "参数传入有误！";
                    return System.Web.HttpUtility.HtmlDecode(XmlHelper.XmlSerialize(esbmsg, true));
                }
            }
            else
            {
                e = new EsbHy();
                e.Ext = "HIS@@123";
                e.Hyyeb = new ESB.HYXT.yw_hyyeb
                {

                    EMPI = "输入要查询的EMPI号",
                    xyjf = 0,
                    xyye = 0,
                    yw_id = Guid.NewGuid()
                };

            }
            #endregion
            string strxml = XmlHelper.XmlSerialize(e, true);

            #region
            DbSession dbSessionDefault = GetDbSession();
            Hxj.Model.yw_hyyeb yeb = dbSessionDefault.From<Hxj.Model.yw_hyyeb>().Where(Hxj.Model.yw_hyyeb._.EMPI == e.Hyyeb.EMPI).ToFirst();
            #endregion

            try
            {
                if (xml == "test")
                {
                    esbmsg.Status = "X";
                    esbmsg.Msg = strxml.Replace("<xyye>0</xyye><xyjf>0</xyjf>", "");
                }
                else
                {
                    esbmsg.Status = "T";
                    if (e.Ext != "HIS@@123")
                    {
                        esbmsg.Msg = "无法使用接口！请联系管理人员！";
                        throw new Exception();
                    }
                    if (yeb == null)
                    {
                        esbmsg.Status = "F";
                        esbmsg.Msg = "需查询的EMPI号不存在！";
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(yeb.FEMPI))
                        {
                            Hxj.Model.yw_hyyeb fyeb = dbSessionDefault.From<Hxj.Model.yw_hyyeb>().Where(Hxj.Model.yw_hyyeb._.EMPI == yeb.FEMPI).ToFirst();
                            if (fyeb == null)
                            {
                                esbmsg.Msg = String.Format("{0}的父卡{1}不存在！", yeb.EMPI, yeb.FEMPI);
                                throw new Exception();
                            }
                            else
                            {
                                yeb.xyye = fyeb.xyye;
                                yeb.xyjf = fyeb.xyjf;

                            }
                        }
                        if (yeb.xyye == null) yeb.xyye = 0;
                        yeb.xyjf = 0;
                        Guid g = Guid.NewGuid();
                        yeb.yw_id = g;
                        string ywid = string.Format("<yw_id>{0}</yw_id>", g);
                        yeb.credit = 0;
                        strxml = XmlHelper.XmlSerialize(yeb, true);
                        esbmsg.Msg = strxml.Replace("<xyjf>0</xyjf>", "").Replace(ywid, "").Replace("<credit>0</credit>", "");

                    }

                    ContextUtil.SetComplete(); //提交事务 
                }
            }
            catch (Exception ex)
            {
                esbmsg.Status = "F";
                if (string.IsNullOrEmpty(esbmsg.Msg))
                {
                    esbmsg.Msg = strxml;// ex.Message;
                }
                ContextUtil.SetAbort(); //回滚事务
            }
            return System.Web.HttpUtility.HtmlDecode(XmlHelper.XmlSerialize(esbmsg, true));
        }

        /// <summary>
        /// 积分查询
        /// </summary>
        /// <param name="xml"></param>
        /// <returns></returns>
        [WebMethod(TransactionOption = TransactionOption.Required)]
        public string HY_Pub_Postjfcx(string xml)
        {
            EsbHy e = null;
            //DbSession dbSessionDefault = new DbSession("ConnectionString");
            // 

            #region 返回信息
            ESB.EsbMsg esbmsg = new EsbMsg();
            #endregion
            #region
            if (!string.IsNullOrEmpty(xml) && xml != "test")
            {
                try
                {
                    e = XmlHelper.XmlDeserialize<EsbHy>(System.Web.HttpUtility.HtmlDecode(xml));
                }
                catch
                {
                    esbmsg.Status = "F";
                    esbmsg.Msg = "参数传入有误！";
                    return System.Web.HttpUtility.HtmlDecode(XmlHelper.XmlSerialize(esbmsg, true));
                }
            }
            else
            {
                e = new EsbHy();
                e.Ext = "HIS@@123";
                e.Hyyeb = new ESB.HYXT.yw_hyyeb
                {

                    EMPI = "输入要查询的EMPI号",
                    xyjf = 0,
                    xyye = 0,
                    yw_id = Guid.NewGuid()
                };

            }
            #endregion
            string strxml = XmlHelper.XmlSerialize(e, true);

            #region
            DbSession dbSessionDefault = GetDbSession();
            Hxj.Model.yw_hyyeb yeb = dbSessionDefault.From<Hxj.Model.yw_hyyeb>().Where(Hxj.Model.yw_hyyeb._.EMPI == e.Hyyeb.EMPI).ToFirst();
            #endregion

            try
            {
                if (xml == "test")
                {
                    esbmsg.Status = "X";
                    esbmsg.Msg = strxml.Replace("<xyye>0</xyye><xyjf>0</xyjf>", "");
                }
                else
                {
                    esbmsg.Status = "T";
                    if (e.Ext != "HIS@@123")
                    {
                        esbmsg.Msg = "无法使用接口！请联系管理人员！";
                        throw new Exception();
                    }
                    if (yeb == null)
                    {
                        esbmsg.Status = "F";
                        esbmsg.Msg = "需查询的EMPI号不存在！";
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(yeb.FEMPI))
                        {
                            Hxj.Model.yw_hyyeb fyeb = dbSessionDefault.From<Hxj.Model.yw_hyyeb>().Where(Hxj.Model.yw_hyyeb._.EMPI == yeb.FEMPI).ToFirst();
                            if (fyeb == null)
                            {
                                esbmsg.Msg = String.Format("{0}的父卡{1}不存在！", yeb.EMPI, yeb.FEMPI);
                                throw new Exception();
                            }
                            else
                            {
                                yeb.xyye = fyeb.xyye;
                                yeb.xyjf = fyeb.xyjf;

                            }
                        }
                        if (yeb.xyjf == null) yeb.xyjf = 0;
                        yeb.xyye = 0;
                        Guid g = Guid.NewGuid();
                        yeb.yw_id = g;
                        string ywid = string.Format("<yw_id>{0}</yw_id>", g);
                        yeb.credit = 0;
                        strxml = XmlHelper.XmlSerialize(yeb, true);
                        esbmsg.Msg = strxml.Replace("<xyye>0</xyye>", "").Replace(ywid, "").Replace("<credit>0</credit>", "");

                    }

                    ContextUtil.SetComplete(); //提交事务 
                }
            }
            catch (Exception ex)
            {
                esbmsg.Status = "F";
                if (string.IsNullOrEmpty(esbmsg.Msg))
                {
                    esbmsg.Msg = strxml;// ex.Message;
                }
                ContextUtil.SetAbort(); //回滚事务
            }
            return System.Web.HttpUtility.HtmlDecode(XmlHelper.XmlSerialize(esbmsg, true));
        }


        /// <summary>
        /// 会员积分接口
        /// </summary>
        /// <param name="xml"></param>
        /// <returns></returns>
        [WebMethod(TransactionOption = TransactionOption.Required)]
        public string HY_Pub_Postjf(string xml)
        {
            EsbHy e = null;
            //DbSession dbSessionDefault = new DbSession("ConnectionString");
            // 

            #region 返回信息
            ESB.EsbMsg esbmsg = new EsbMsg();
            #endregion
            #region
            if (!string.IsNullOrEmpty(xml) && xml != "test")
            {
                try
                {
                    e = XmlHelper.XmlDeserialize<EsbHy>(System.Web.HttpUtility.HtmlDecode(xml));
                }
                catch
                {
                    esbmsg.Status = "F";
                    esbmsg.Msg = "参数传入有误！";
                    return System.Web.HttpUtility.HtmlDecode(XmlHelper.XmlSerialize(esbmsg, true));
                }
            }
            else
            {
                e = new EsbHy();
                e.Ext = "HIS@@123";
                e.Hyyeb = new ESB.HYXT.yw_hyyeb
                {

                    EMPI = "输入要查询的EMPI号",
                    xyjf = 0,
                    xyye = 0,
                    yw_id = Guid.NewGuid()
                };

            }
            #endregion
            string strxml = XmlHelper.XmlSerialize(e, true);

            #region
            DbSession dbSessionDefault = GetDbSession();
            Hxj.Model.yw_hyyeb yeb = dbSessionDefault.From<Hxj.Model.yw_hyyeb>().Where(Hxj.Model.yw_hyyeb._.EMPI == e.Hyyeb.EMPI).ToFirst();
            #endregion

            try
            {
                if (xml == "test")
                {
                    esbmsg.Status = "X";
                    esbmsg.Msg = strxml.Replace("<xyye>0</xyye><xyjf>0</xyjf>", "");
                }
                else
                {
                    esbmsg.Status = "T";
                    if (e.Ext != "HIS@@123")
                    {
                        esbmsg.Msg = "无法使用接口！请联系管理人员！";
                        throw new Exception();
                    }
                    if (yeb == null)
                    {
                        esbmsg.Status = "F";
                        esbmsg.Msg = "需查询的EMPI号不存在！";
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(yeb.FEMPI))
                        {
                            Hxj.Model.yw_hyyeb fyeb = dbSessionDefault.From<Hxj.Model.yw_hyyeb>().Where(Hxj.Model.yw_hyyeb._.EMPI == yeb.FEMPI).ToFirst();
                            if (fyeb == null)
                            {
                                esbmsg.Msg = String.Format("{0}的父卡{1}不存在！", yeb.EMPI, yeb.FEMPI);
                                throw new Exception();
                            }
                            else
                            {
                                yeb.xyye = fyeb.xyye;
                                yeb.xyjf = fyeb.xyjf;

                            }
                        }
                        yeb.xyjf = 0;
                        strxml = XmlHelper.XmlSerialize(yeb, true);
                        esbmsg.Msg = strxml.Replace("<xyye>0</xyye>", "");

                    }

                    ContextUtil.SetComplete(); //提交事务 
                }
            }
            catch (Exception ex)
            {
                esbmsg.Status = "F";
                if (string.IsNullOrEmpty(esbmsg.Msg))
                {
                    esbmsg.Msg = strxml;// ex.Message;
                }
                ContextUtil.SetAbort(); //回滚事务
            }
            return System.Web.HttpUtility.HtmlDecode(XmlHelper.XmlSerialize(esbmsg, true));
        }

        /// <summary>
        /// 会员积分兑换接口
        /// </summary>
        /// <param name="xml"></param>
        /// <returns></returns>
        [WebMethod(TransactionOption = TransactionOption.Required)]
        public string HY_Pub_Postjfdh(string xml)
        {
            return "";
        }

        /// <summary>
        /// 消费查询接口
        /// </summary>
        /// <param name="xml"></param>
        /// <returns></returns>
        [WebMethod(TransactionOption = TransactionOption.Required)]
        public string HY_Pub_Postxfcx(string xml)
        {
            return "";
        }

        /// <summary>
        /// 支付查询接口
        /// </summary>
        /// <param name="xml"></param>
        /// <returns></returns>
        [WebMethod(TransactionOption = TransactionOption.Required)]
        public string HY_Pub_Postzfcx(string xml)
        {
            return "";
        }

        /// <summary>
        /// 门诊消费接口
        /// </summary>
        /// <param name="xml"></param>
        /// <returns></return>
        [WebMethod(TransactionOption = TransactionOption.Required)]
        public string YW_Pub_Postmzxf(string xml)
        {
            EsbYw e = null;
            #region 返回信息
            ESB.EsbMsg esbmsg = new EsbMsg();
            #endregion
            #region
            if (!string.IsNullOrEmpty(xml) && xml != "test")
            {
                try
                {
                    e = XmlHelper.XmlDeserialize<EsbYw>(System.Web.HttpUtility.HtmlDecode(xml));
                }
                catch
                {
                    esbmsg.Status = "F";
                    esbmsg.Msg = "参数传入有误！";
                    return System.Web.HttpUtility.HtmlDecode(XmlHelper.XmlSerialize(esbmsg, true));
                }
            }
            else
            {
                e = new EsbYw();
                List<YW_xfjlmxb> xfmx = new List<YW_xfjlmxb>();
                xfmx.Add(new YW_xfjlmxb { xfxm = "消费项目1", je = 50 });
                xfmx.Add(new YW_xfjlmxb { xfxm = "消费项目2", je = 100 });

                List<yw_zfjlmxb> zfmxb = new List<yw_zfjlmxb>();
                zfmxb.Add(new yw_zfjlmxb { zflx = "支付类型1", zfje = 50 });
                zfmxb.Add(new yw_zfjlmxb { zflx = "支付类型2", zfje = 100 });
                e.Xfjlzb = new YW_xfjlzb
                    {
                        xfdh = "消费单号",
                        xflx = "消费类型",
                        EMPI = "EMPI",
                        ksid = "科室ID",
                        ysid = "医生ID",
                        yzid = "驿站ID",
                        kh = "会员卡号",
                        xfsj = DateTime.Now,
                        Mxb = xfmx
                    };
                e.Zfjlzb = new YW_zfjlzb { Mxb = zfmxb, xfdh = "消费单号", zfsj = DateTime.Now };
            }
            #endregion
            string strxml = XmlHelper.XmlSerialize(e, true);

            #region
            YW.Model.YWXT.YW_xfjlzb model = new YW.Model.YWXT.YW_xfjlzb();

            model.xfdh = e.Xfjlzb.xfdh;
            model.xflx = e.Xfjlzb.xflx;
            model.EMPI = e.Xfjlzb.EMPI;
            model.ksid = e.Xfjlzb.ksid;
            model.ysid = e.Xfjlzb.ysid;
            model.yzid = e.Xfjlzb.yzid;
            model.xfsj = e.Xfjlzb.xfsj;
            model.kh = e.Xfjlzb.ksid;
            model.addtime = e.Xfjlzb.addtime;
            #endregion

            try
            {
                if (xml == "test")
                {
                    esbmsg.Status = "X";
                    esbmsg.Msg = strxml;
                }
                else
                {
                    esbmsg.Status = "T";
                    YW.BLL.YWXT.YW_xfjlzb bll = new YW.BLL.YWXT.YW_xfjlzb();
                    esbmsg.Msg = "1";
                    bll.Add(model);
                    //model.kh = "2222";
                    model.kh = "卡号";
                    bll.Add(model);
                    esbmsg.Msg = strxml;
                    ContextUtil.SetComplete(); //提交事务 
                }
            }
            catch (Exception ex)
            {
                esbmsg.Status = "F";
                esbmsg.Msg = strxml;// ex.Message;
                ContextUtil.SetAbort(); //回滚事务
            }
            return System.Web.HttpUtility.HtmlDecode(XmlHelper.XmlSerialize(esbmsg, true));
        }

        //[WebMethod(TransactionOption = TransactionOption.Required)]
        //public string Yw_Jf(string xml)
        //{
        //    #region 返回信息
        //    ESB.EsbMsg esbmsg = new EsbMsg();
        //    ESB.EsbJfMsg s = null;
        //    #endregion
        //    try
        //    {
        //        s = BMS.CheckJf("lhm", "100", "900");
        //        esbmsg.Status = s.Status;
        //        esbmsg.Msg = s.Msg;
        //    }
        //    catch (Exception ex)
        //    {
        //        esbmsg.Status = "F";
        //        if (string.IsNullOrEmpty(esbmsg.Msg))
        //        {
        //            esbmsg.Msg = s.Msg;
        //        }
        //        ContextUtil.SetAbort(); //回滚事务
        //    }
        //    return System.Web.HttpUtility.HtmlDecode(XmlHelper.XmlSerialize(esbmsg, true));


        //}

        //[WebMethod(TransactionOption = TransactionOption.Required)]
        //public string HY_Pub_PostJf(string xml)
        //{
        //    Cz e = null;
        //    #region 返回信息
        //    ESB.EsbMsg esbmsg = new EsbMsg();
        //    #endregion
        //    #region
        //    if (!string.IsNullOrEmpty(xml) && xml != "test")
        //    {
        //        try
        //        {
        //            e = XmlHelper.XmlDeserialize<Cz>(System.Web.HttpUtility.HtmlDecode(xml));
        //        }
        //        catch
        //        {
        //            esbmsg.Status = "F";
        //            esbmsg.Msg = "参数传入有误！";
        //            return System.Web.HttpUtility.HtmlDecode(XmlHelper.XmlSerialize(esbmsg, true));
        //        }
        //    }
        //    else
        //    {
        //        e = new Cz();
        //        e.Ext = "HIS@@123";
        //        e.Empi = "bfd111e1-bf7b-4971-aa73-5da61cca70d4";
        //        e.Je = 1;
        //        e.Jf = 1;
        //        e.Type = 5;
        //        e.Memo = GetMemo() + @"Action:1001 代表预扣款,1002 代表确认扣款";
        //        e.Action = "1001";
        //        e.HisLSH = "HIS结帐编号";

        //    }
        //    #endregion
        //    string strxml = XmlHelper.XmlSerialize(e, true);

        //    #region
        //    DbSession dbSessionDefault = GetDbSession();
        //    Hxj.Model.yw_hyyeb yeb = dbSessionDefault.From<Hxj.Model.yw_hyyeb>().Where(Hxj.Model.yw_hyyeb._.EMPI == e.Empi).ToFirst();

        //    //YW.Model.HYXT.yw_hyyeb model = new YW.Model.HYXT.yw_hyyeb();

        //    //model.EMPI = e.Hyyeb.EMPI;
        //    //model.xyjf = e.Hyyeb.xyjf;
        //    //model.xyye = e.Hyyeb.xyye;
        //    //model.yw_id = e.Hyyeb.yw_id;
        //    #endregion
        //    DbTrans trans = dbSessionDefault.BeginTransaction();
        //    try
        //    {
        //        if (xml == "test")
        //        {
        //            esbmsg.Status = "X";
        //            esbmsg.Msg = strxml;
        //        }
        //        else
        //        {
        //            esbmsg.Status = "T";
        //            if (e.Ext != "HIS@@123")
        //            {
        //                esbmsg.Msg = "无法使用接口！请联系管理人员！";
        //                throw new Exception();
        //            }
        //            if (string.IsNullOrEmpty(e.HisLSH))
        //            {
        //                esbmsg.Msg = "HIS结帐编号不能为空!";
        //                throw new Exception();
        //            }
        //            if (e.Je < 0)
        //            {
        //                esbmsg.Msg = "金额不能为负！";
        //                throw new Exception();
        //            }
        //            if (e.Type != 5 && e.Type != 6)
        //            {
        //                esbmsg.Msg = "消费类型参数有误，5 His门诊预缴费,6 His住院预缴费";
        //                throw new Exception();
        //            }

        //            if (yeb == null)
        //            {
        //                esbmsg.Status = "F";
        //                esbmsg.Msg = "EMPI号不存在！";
        //            }
        //            else
        //            {
        //                #region
        //                ESB.EsbJfMsg s = null;
        //                s = BMS.CheckJf(e.Empi, e.Jf.ToString(), e.Je.ToString());
        //                esbmsg.Status = s.Status;
        //                esbmsg.Msg = s.Msg;
        //                if (s.Status == "F")
        //                {
        //                    throw new Exception();
        //                }
        //                #endregion
        //                if (!string.IsNullOrEmpty(yeb.FEMPI))
        //                {
        //                    Hxj.Model.yw_hyyeb fyeb = dbSessionDefault.From<Hxj.Model.yw_hyyeb>().Where(Hxj.Model.yw_hyyeb._.EMPI == yeb.FEMPI).ToFirst();
        //                    if (fyeb == null)
        //                    {
        //                        esbmsg.Msg = String.Format("{0}的父卡{1}不存在！", yeb.EMPI, yeb.FEMPI);
        //                        throw new Exception();
        //                    }
        //                    else
        //                    {
        //                        yeb.xyye = fyeb.xyye;
        //                        yeb.xyjf = fyeb.xyjf;
        //                    }
        //                }
        //                if (e.Action == "1001")
        //                {
        //                    #region 1001
        //                    if (dbSessionDefault.Exists<HM.Yw_Trial>(HM.Yw_Trial._.xgdjh == e.HisLSH + e.Type.ToString() && HM.Yw_Trial._.type == 1))
        //                    {
        //                        esbmsg.Msg = "单据已付款成功，不能再试算！";
        //                        throw new Exception();
        //                    }
        //                    HM.Yw_Trial trial = dbSessionDefault.From<HM.Yw_Trial>().Where(HM.Yw_Trial._.xgdjh == e.HisLSH + e.Type.ToString() && HM.Yw_Trial._.type == 0).ToFirst();
        //                    if (trial == null)
        //                    {
        //                        trial = new HM.Yw_Trial();
        //                        trial.yw_id = Guid.NewGuid();
        //                        trial.xgdjh = e.HisLSH + e.Type.ToString();
        //                        trial.balance = yeb.xyye;
        //                        trial.receipt = e.Je;
        //                        #region 折扣额和积分现在待定
        //                        trial.discount = e.Je - decimal.Parse(s.Money);
        //                        trial.integral = e.Jf;
        //                        #endregion
        //                        trial.received = e.Je - trial.discount;
        //                        if (yeb.xyye < trial.received)
        //                        {
        //                            trial.card = yeb.xyye;
        //                            trial.cash = trial.received - yeb.xyye;
        //                        }
        //                        else
        //                        {
        //                            trial.card = trial.received;
        //                            trial.cash = 0;
        //                        }
        //                        trial.type = 0;
        //                        trial.EMPI = yeb.EMPI;
        //                        trial.FEMPI = yeb.FEMPI;
        //                        trial.ext = e.Ext.Split(new char[] { '@' })[0].ToString();
        //                        int returnvalue = dbSessionDefault.Insert<HM.Yw_Trial>(trial, trans);
        //                        if (returnvalue > 0)
        //                        {
        //                            e.Action = "1002";
        //                            if (e.trial == null)
        //                            {
        //                                e.trial = new Trial();
        //                            }
        //                            e.trial.BCJE = trial.receipt;
        //                            e.trial.ZHZF = trial.card;
        //                            e.trial.XJZF = trial.cash;
        //                            e.trial.YHJE = trial.discount;
        //                            e.trial.ZHYE = trial.balance;
        //                            e.trial.JF = trial.integral;
        //                            e.trial.ZFID = trial.yw_id.ToString();
        //                            esbmsg.Msg = XmlHelper.XmlSerialize(e, true);
        //                        }
        //                        else
        //                        {
        //                            esbmsg.Msg = "试算有误！";
        //                            throw new Exception();
        //                        }
        //                    }
        //                    else
        //                    {
        //                        trial.Attach();
        //                        //                                receipt decimal 9 2     √   本次收款金额 
        //                        //3 discount decimal 9 2     √   折扣 
        //                        //4 received decimal 9 2     √   实收金额 
        //                        //5 card decimal 9 2     √   卡中款处理 
        //                        //6 cash decimal 9 2     √   现金 
        //                        //7 balance decimal 9 2     √   卡内余额 
        //                        //8 type int 4 0     √   状态:0代表试算，1代表付款，2代表退款 
        //                        //9 integral decimal 9 0     √     
        //                        trial.balance = yeb.xyye;
        //                        trial.receipt = e.Je;
        //                        trial.updatetime = DateTime.Now;
        //                        #region 折扣额和积分现在待定
        //                        trial.discount = e.Je - decimal.Parse(s.Money);
        //                        trial.integral = e.Jf;
        //                        #endregion
        //                        trial.received = e.Je - trial.discount;
        //                        if (yeb.xyye < trial.received)
        //                        {
        //                            trial.card = yeb.xyye;
        //                            trial.cash = trial.received - yeb.xyye;
        //                        }
        //                        else
        //                        {
        //                            trial.card = trial.received;
        //                            trial.cash = 0;
        //                        }
        //                        trial.EMPI = yeb.EMPI;
        //                        trial.FEMPI = yeb.FEMPI;
        //                        trial.ext = e.Ext.Split(new char[] { '@' })[0].ToString();
        //                        int returnvalue = dbSessionDefault.Update<HM.Yw_Trial>(trial, trans);
        //                        if (returnvalue > 0)
        //                        {
        //                            e.Action = "1002";
        //                            if (e.trial == null) e.trial = new Trial();
        //                            e.trial.BCJE = trial.receipt;
        //                            e.trial.ZHZF = trial.card;
        //                            e.trial.XJZF = trial.cash;
        //                            e.trial.YHJE = trial.discount;
        //                            e.trial.ZHYE = trial.balance;
        //                            e.trial.JF = trial.integral;
        //                            e.trial.ZFID = trial.yw_id.ToString();
        //                            esbmsg.Msg = XmlHelper.XmlSerialize(e, true);
        //                        }
        //                        else
        //                        {
        //                            esbmsg.Msg = "重新试算有误！";
        //                            throw new Exception();
        //                        }

        //                    }
        //                    #endregion
        //                }
        //                else if (e.Action == "1002")
        //                {
        //                    if (e.trial == null)
        //                    {
        //                        esbmsg.Msg = "参数传入有误！";
        //                        throw new Exception();
        //                    }
        //                    if (dbSessionDefault.Exists<HM.Yw_Trial>(HM.Yw_Trial._.xgdjh == e.HisLSH + e.Type.ToString() && HM.Yw_Trial._.type == 1))
        //                    {
        //                        esbmsg.Msg = "单据已付款成功，不能再试算！";
        //                        throw new Exception();
        //                    }
        //                    if (!dbSessionDefault.Exists<HM.Yw_Trial>(HM.Yw_Trial._.xgdjh == e.HisLSH + e.Type.ToString() && HM.Yw_Trial._.type == 0))
        //                    {
        //                        esbmsg.Msg = "请选试算！";
        //                        throw new Exception();
        //                    }
        //                    if (!dbSessionDefault.Exists<HM.Yw_Trial>(HM.Yw_Trial._.xgdjh == e.HisLSH + e.Type.ToString() && HM.Yw_Trial._.type == 0 && HM.Yw_Trial._.yw_id == e.trial.ZFID))
        //                    {
        //                        esbmsg.Msg = "二次握手不成功！";
        //                        throw new Exception();
        //                    }
        //                    HM.Yw_Trial trial = dbSessionDefault.From<HM.Yw_Trial>().Where(HM.Yw_Trial._.xgdjh == e.HisLSH + e.Type.ToString() && HM.Yw_Trial._.type == 0 && HM.Yw_Trial._.yw_id == e.trial.ZFID).ToFirst();
        //                    if (trial != null)
        //                    {
        //                        if (
        //                        e.trial.BCJE == trial.receipt &&
        //                        e.trial.ZHZF == trial.card &&
        //                        e.trial.XJZF == trial.cash &&
        //                        e.trial.YHJE == trial.discount &&
        //                        e.trial.ZHYE == trial.balance &&
        //                        e.trial.JF == trial.integral && e.Empi == trial.EMPI)
        //                        {
        //                            #region

        //                            #endregion
        //                            Guid bgid = Guid.NewGuid();
        //                            trial.Attach();
        //                            trial.type = 1;
        //                            //trial.xgdjh = bgid.ToString();
        //                            int i = dbSessionDefault.Update<HM.Yw_Trial>(trial, trans);
        //                            if (i > 0)
        //                            {
        //                                esbmsg.Msg = "ok";
        //                                #region 扣款，写日志记录
        //                                yeb.Attach();
        //                                #region
        //                                if (!string.IsNullOrEmpty(yeb.FEMPI))
        //                                {
        //                                    Hxj.Model.yw_hyyeb fyeb = dbSessionDefault.From<Hxj.Model.yw_hyyeb>().Where(Hxj.Model.yw_hyyeb._.EMPI == yeb.FEMPI).ToFirst();
        //                                    if (fyeb == null)
        //                                    {
        //                                        esbmsg.Msg = String.Format("{0}的父卡{1}不存在！", yeb.EMPI, yeb.FEMPI);
        //                                        throw new Exception();
        //                                    }
        //                                    else
        //                                    {
        //                                        yeb.xyye = fyeb.xyye;
        //                                        yeb.xyjf = fyeb.xyjf;
        //                                    }
        //                                }
        //                                #endregion
        //                                decimal? d = yeb.xyye;
        //                                decimal? d1 = yeb.xyye - trial.card;
        //                                yeb.xyye = yeb.xyye - trial.card;
        //                                yeb.xyjf = yeb.xyjf - trial.integral;
        //                                if (d != d1)
        //                                {
        //                                    if (string.IsNullOrEmpty(yeb.FEMPI))
        //                                    {
        //                                        i = dbSessionDefault.Update<HM.yw_hyyeb>(yeb, HM.yw_hyyeb._.EMPI == e.Empi && HM.yw_hyyeb._.xyye == trial.balance, trans);
        //                                    }
        //                                    else
        //                                    {
        //                                        i = dbSessionDefault.Update<HM.yw_hyyeb>(yeb, HM.yw_hyyeb._.EMPI == yeb.FEMPI && HM.yw_hyyeb._.xyye == trial.balance, trans);
        //                                    }
        //                                }
        //                                else
        //                                    i = 1;
        //                                if (i > 0)
        //                                {
        //                                    esbmsg.Msg = "ok";
        //                                    #region 写余额变更记录
        //                                    Hxj.Model.YW_yebgjl bgjl = new HM.YW_yebgjl();
        //                                    bgjl.Attach();
        //                                    bgjl.yw_id = bgid;
        //                                    bgjl.EMPI = e.Empi;
        //                                    bgjl.FEMPI = yeb.FEMPI;
        //                                    bgjl.bgje = e.trial.ZHZF;
        //                                    bgjl.cash = e.trial.XJZF;
        //                                    bgjl.integral = e.trial.JF;
        //                                    bgjl.receipt = e.trial.BCJE;
        //                                    bgjl.bglx = e.Type.ToString();
        //                                    bgjl.xgdjh = e.HisLSH;
        //                                    bgjl.ext = e.Ext.Split(new char[] { '@' })[0].ToString();
        //                                    bgjl.trialid = new Guid(e.trial.ZFID);
        //                                    i = dbSessionDefault.Insert<Hxj.Model.YW_yebgjl>(bgjl, trans);
        //                                    if (i > 0)
        //                                    {
        //                                        e.trial.ZFID = bgid.ToString();
        //                                        esbmsg.Msg = XmlHelper.XmlSerialize(e, true);
        //                                    }
        //                                    else
        //                                    {
        //                                        esbmsg.Msg = "确认扣款不成功！写余额变更记录的时候出错！";
        //                                        throw new Exception();
        //                                    }

        //                                    #endregion

        //                                }
        //                                else
        //                                {
        //                                    esbmsg.Msg = "请重新试算！确认扣款不成功！会员余额和试算的时候不一致！";
        //                                    throw new Exception();
        //                                }
        //                                #endregion
        //                            }
        //                            else
        //                            {
        //                                esbmsg.Msg = "确认扣款不成功！";
        //                                throw new Exception();
        //                            }
        //                        }
        //                        else
        //                        {
        //                            esbmsg.Msg = "数据发生变更，请重新试算！";
        //                            throw new Exception();
        //                        }
        //                    }
        //                    else
        //                    {
        //                        esbmsg.Msg = "....！";
        //                        throw new Exception();
        //                    }
        //                }
        //                else
        //                {
        //                    esbmsg.Msg = "Action参数有误！";
        //                    throw new Exception();
        //                }
        //                ////修改会员余额
        //                //YW.Model.HYXT.yw_hyyeb hyyeb = new YW.Model.HYXT.yw_hyyeb();
        //                //hyyeb.EMPI = yeb.EMPI;
        //                //hyyeb.yw_id = yeb.yw_id;
        //                //hyyeb.xyye = yeb.xyye - e.Je;
        //                //hyyeb.xyjf = yeb.xyjf;
        //                //YW.BLL.HYXT.yw_hyyeb bll = new YW.BLL.HYXT.yw_hyyeb();
        //                //bll.Update(hyyeb);

        //                ////增加日志记录
        //                //YW.Model.HYXT.YW_yebgjl yebgjl = new YW.Model.HYXT.YW_yebgjl();
        //                //yebgjl.yw_id = Guid.NewGuid();
        //                //yebgjl.ywlx = e.Type.ToString();
        //                //yebgjl.EMPI = e.Empi;
        //                //yebgjl.bgje = -e.Je;
        //                //yebgjl.bglx = e.Type.ToString();
        //                //YW.BLL.HYXT.YW_yebgjl yebgbll = new YW.BLL.HYXT.YW_yebgjl();
        //                //yebgbll.Add(yebgjl);

        //                strxml = XmlHelper.XmlSerialize(e, true);
        //            }

        //            //ContextUtil.SetComplete(); //提交事务 
        //            trans.Commit();
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        esbmsg.Status = "F";
        //        if (string.IsNullOrEmpty(esbmsg.Msg)) esbmsg.Msg = strxml;// ex.Message;
        //        //ContextUtil.SetAbort(); //回滚事务
        //        trans.Rollback();
        //    }
        //    finally
        //    {
        //        trans.Close();
        //    }
        //    return System.Web.HttpUtility.HtmlDecode(XmlHelper.XmlSerialize(esbmsg, true));
        //}
        
    }
}